<?php
/**
 * Created by 2019/4/11 0011
 * Effect: ApiBaseController.php
 * Author: 品花
 * Date: 2019/4/11 0011
 * Time: 下午 1:48
 */
namespace App\HttpController\Api\V1;


use App\Consts\RedisKey;
use App\HttpController\BaseController;
use App\Model\Pool;
use App\Model\User;
use App\Model\UserStarCoinRecord;
use App\Utility\Register;
use App\Utility\Response;
use App\Validate\ValidateTool;
use EasySwoole\Http\Message\Status;
use EasySwoole\RedisPool\Redis;

class ApiBaseController extends BaseController
{


    public function index()
    {
        // TODO: Implement index() method.
    }



    /**
     * 验证参数
     * @param string|null $action
     * @return bool|null
     *
     */
    protected function onRequest(?string $action): ?bool
    {

        $ret =  parent::onRequest($action);

        if($ret === false){
            return false;
        }

        $param = $this->request()->getRequestParam();

        //获取自动验证规则
        $Validate = ValidateTool::validateRule($this->request()->getUri()->getPath(),$param);

        if($Validate){
            $ret = $Validate->validate($param);

            if($ret == false){
                $this->returnData(999,['param_name'=> $Validate->getError()->getField()],$Validate->getError()->__toString());
                return false;
            }
        }
        return true;
    }


    protected function getUserId(){

        return Register::getInstance()->get('uid');
    }

    protected function getUserInfo(){

        return Register::getInstance()->get('user_info');
    }

    protected function isBindWechat(){

        return User::create()->where(['id'=> $this->getUserId()])->scalar('app_openid')?true:false;
    }

    protected function getNowUserInfo(){

        return User::create()->where(['id'=> $this->getUserId()])->get();
    }

    protected function getNowUserForUpdate(){

        return User::create()->where(['id'=> $this->getUserId()])->get(function ($builder){
            $builder->selectForUpdate();

        });
    }
    
    protected function getUserForUpdate($id){
        
        return User::create()->where(['id'=> $id])->get(function ($builder){
            $builder->selectForUpdate();
            
        });
    }





}